package com.pan.crawler.impl7;


import com.pan.crawler.Page;
import com.pan.entity.Chapter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class PageImpl7 implements Page {

    private final String webUrl = "www.beqege.cc";

    private EdgeDriver edgeDriver;
    private WebDriverWait webDriverWait;
    public PageImpl7() {
        this.edgeDriver = new EdgeDriver();
        edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
//        webDriverWait = new WebDriverWait(edgeDriver,Duration.ofSeconds(5));
    }

    @Override
    public String get(Chapter chapter) {
        String url = chapter.getUrl();
        if (url.indexOf(webUrl) == -1){
            throw new RuntimeException("该网站不是："+webUrl);
        }
        edgeDriver.navigate().to(url);
//        StringBuffer stringBuffer = new StringBuffer();
        WebElement contentDivElement = edgeDriver.findElement(By.id("content"));
        //这样写开销可能有点大
        /*
        for (WebElement pElement : contentDivElement.findElements(By.tagName("p"))) {
            String pText = pElement.getText();
            stringBuffer.append(pText);
            stringBuffer.append("\n");
        }
        return stringBuffer.toString();
        */
        //尝试改成这样
        //经过测试，貌似这种写法并没有使开销降低多少
        String text = contentDivElement.getText();
        String del = "【告知书友，时代在变化，免费站点难以长存，手机app多书源站点切换看书大势所趋，站长给你推荐的这个换源APP，听书音色多、换源、找书都好使！】";
        int delLen = del.length();
        return text.trim().substring(delLen, text.length()- delLen);
    }

    @Override
    public void close() {
        edgeDriver.quit();
    }
}
